nodenum = 36;
nodeflagperl = new Array();
for(i=0;i<nodenum;i++)
{
	nodeflagperl[i] = 1;
	//1:wait for burning 
	//0:seleted to burn
	//2:blink
	//3:done successfully
	//4:done unsuccessfully
}



function selectOnes()
{

}


function set_dot(i,j)// when clicked ,change color of dots
{
	var c=document.getElementById("coordiv");
	var cxt=c.getContext("2d");
	cxt.fillStyle="#A9A9A9";
	cxt.beginPath();
	cxt.arc(15+i*40,15+j*40,15,0,Math.PI*2,true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle="#000000";
	cxt.font="16px Arial";
	cxt.fillText(""+(i*10+j),15+i*40-7,15+j*40+4);
}

function cancel_dot(i,j)// when clicked ,change color of dots
{
	var c=document.getElementById("coordiv");
	var cxt=c.getContext("2d");
	cxt.fillStyle="#FF0000";
	cxt.beginPath();
	cxt.arc(15+i*40,15+j*40,15,0,Math.PI*2,true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle="#000000";
	cxt.font="16px Arial";
	cxt.fillText(""+(i*10+j),15+i*40-7,15+j*40+4);
}
function checkID()
{
	if(document.myform.showid.value=="")
	{
		alert("Please choose a node at least!");
		return false;
	}
	var str=prompt("Please input password","******");
	if(str!="tinyos")
	{
		alert("Wrong Password!");
		return false;
	}

	return true;
}

function stateChanged()
{
	if (xmlHttp.readyState==4)
	{
	//	set_dot(6,6);
		//alert(xmlHttp.responseText);
	}
}


function checkblinkfuc(nodestr){
	var nodestrarray = new Array();
	nodestrarray= nodestr.split(",");
	var num;
	var count;
	for(count=0;count<nodestrarray.length;count++){
		num = parseInt(nodestrarray[count]);
		nodeflagperl[num]=2;
	}

}

function blinkfuc1(flag,nodeid){//flag==1:#ff0000 0:#00ff00
	var c=document.getElementById("coordiv");
	var cxt=c.getContext("2d");
	var i= Math.floor(nodeid/10);
	var j= nodeid%10; 
	if(flag==1){
		cxt.fillStyle="#FF0000";
	}else if(flag==0)
	{
		cxt.fillStyle="#00FF00";
	}else if(flag==3)
	{
		cxt.fillStyle="#00FF00";
	}else if(flag==4){
		cxt.fillStyle="#0000FF";
	}
cxt.beginPath();
cxt.arc(15+i*40,15+j*40,15,0,Math.PI*2,true);
cxt.closePath();
cxt.fill();
cxt.fillStyle="#000000";
cxt.font="16px Arial";
cxt.fillText(""+(i*10+j),15+i*40-7,15+j*40+4);
}

flag=0;
function blinkallfuc(){
	flag++;
	var k=0;
	for(k=0;k<nodenum;k++){
		if(nodeflagperl[k]==2){
			blinkfuc1(flag%2,k);
		}
	}
}
function returnstate(responseText)
{

	//debug
	/*
	var str=null;
        for(count=0;count<nodenum;count++){
	str+=nodeflagperl[count]+",";
	}
	*/
//	alert(str);

	var responseArray =  new Array();
	responseArray= responseText.split(",");
	var num;
	var count;
	for(count=0;count<responseArray.length;count+=2){
		num = parseInt(responseArray[count]);
		nodeflagperl[num]=parseInt(responseArray[count+1])+3;
		//change states ,so 0->3,1->4
	}
	//debug
	/*
	var str=null;
        for(count=0;count<nodenum;count++){
	str+=nodeflagperl[count]+",";
}	*/
//	alert(str);

}

function showstate()
{
	var count=0;
	for(count=0;count<nodenum;count++){
		if(nodeflagperl[count]==3){
			blinkfuc1(3,count);
		}else if(nodeflagperl[count]==4){
			blinkfuc1(4,count);
		}
	}
}

function checkIDandOthers()
{

	if(document.myform.showid.value=="")
	{
		alert("Please choose a node at least!");
		return false;
	}
	var str=prompt("Please input password","******");
	if(str!="tinyos")
	{
		alert("Wrong Password!");
		return false;
	}
	var xmlHttp=new XMLHttpRequest();
	if (xmlHttp==null)
	{
		alert ("no support AJAX！");
		return;
	}
	var url="burn.jsp";

	var showidvalue=document.myform.showid.value
		url=url+"?showid="+showidvalue;
	checkblinkfuc(showidvalue);
	Timer = setInterval("blinkallfuc()",200);
	//set_dot(4,4);
	xmlHttp.onreadystatechange=function()
	{
	//	set_dot(5,5);
		if (xmlHttp.readyState==4 && xmlHttp.status==200)
		{
		//	set_dot(3,3);
		//	alert(xmlHttp.responseText);
			clearInterval(Timer);
		//	set_dot(7,7);
			returnstate(xmlHttp.responseText);
			showstate();
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return true;
}

function show()   //show init color
{
  var i,j;
  var c=document.getElementById("coordiv");
  var cxt=c.getContext("2d");
  for(i = 6 ; i>=0 ; i--)   //7 columns , 5 rows
  {
    for( j = 4 ; j>=0 ; j--)
    {
      cxt.fillStyle="#A9A9A9";
      cxt.beginPath();
      cxt.arc(15+i*40,15+j*40,15,0,Math.PI*2,true);
      cxt.closePath();
      cxt.fill();
      cxt.fillStyle="#000000";
      cxt.font="16px Arial";
      cxt.fillText(""+(i*5+j + 1),15+i*40-7,15+j*40+4);
    }
  }
  cxt.fillStyle="#A9A9A9";
  cxt.beginPath();
  cxt.arc(15+3*40,15+5*40,15,0,Math.PI*2,true);
  cxt.closePath();
  cxt.fill();
  cxt.fillStyle="#000000";
  cxt.font="16px Arial";
  cxt.fillText(""+0,15+3*40-7,15+5*40+4);
}

function getNode()
{
  var str="";
  for(i=0;i<nodenum;i++)
  {
    if(nodeflagperl[i]==0)
    {
      str+=i;
      str+=",";
    }
  }
  var c=document.getElementById('showid');
  c.value=str; 
}

function cnvs_getCoordinates(e) //when clicked ,get i,j
{
  x=e.clientX-100;
  y=e.clientY-nodenum;

  for(var i=0;i<8;i++)
  {
    for(var j=0;j<10;j++)
    {
      tmpx=15+40*i;
      tmpy=15+40*j;
      if(((x-tmpx)*(x-tmpx)+(y-tmpy)*(y-tmpy))<=15*15)
      {
        var nodeid=i*10+j;
        if(nodeflagperl[nodeid]==1)
        {
          nodeflagperl[nodeid]=0;
          cancel_dot(i,j);
        }
        else if(nodeflagperl[nodeid]==0)
        {
          nodeflagperl[nodeid]=1;
          set_dot(i,j);
        }


        break;
      }
    }

  }
  document.getElementById("xycoordinates1").innerHTML="Clicked Node ID:" + nodeid ;
  //	document.getElementById("xycoordinates").innerHTML="Clicked Node ID:::" + nodeid ;
}


function sensor_show(e) //when clicked ,get i,j
{
  x=e.clientX-100;
  y=e.clientY-nodenum;

  for(var i=0;i<8;i++)
  {
    for(var j=0;j<10;j++)
    {
      tmpx=15+40*i;
      tmpy=15+40*j;
      if(((x-tmpx)*(x-tmpx)+(y-tmpy)*(y-tmpy))<=15*15)
      {
        var nodeid=i*10+j;
        if(nodeflagperl[nodeid]==1)
        {
          nodeflagperl[nodeid]=0;
          cancel_dot(i,j);
        }
        else if(nodeflagperl[nodeid]==0)
        {
          nodeflagperl[nodeid]=1;
          set_dot(i,j);
        }


        break;
      }
    }

  }
}
function cnvs_clearCoordinates()  //when leave are ,clear x,y
{
  document.getElementById("xycoordinates").innerHTML="";
}
function printData(e)
{

  document.getElementById("xycoordinates").innerHTML="ccc";
}

